package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: SGS
 * @Date : 2020/11/15 16:40
 * @Version :
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    @Autowired
    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 根据品牌id查询品牌
     *
     * @param id 品牌id
     * @return 根据id查询到的品牌
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    /**
     * 根据id集合查询品牌集合
     *
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandsByIds(@RequestParam("ids") List<Long> ids) {
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brandService.listByIds(ids));
        return ResponseEntity.ok(brandDTOS);
    }

    /**
     * 分页查询品牌信息
     *
     * @param page 当前页
     * @param rows 每页数量
     * @param key  关键词
     * @return 品牌信息集合
     */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                                              @RequestParam(value = "key", required = false) String key) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key));
    }

    /**
     * 通过分类id查询所有的品牌
     * @param id 分类id
     * @return 返回品牌信息
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id) {
        return ResponseEntity.ok( brandService.queryBrandByCategory(id));
    }

    /**
     * 新增品牌
     * @param brandDTO 新增品牌对象
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据品牌id更新
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
